# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Marcin Sielski <marcin.sielski@motorolasolutions.com>

_realname=gst-plugins-bad
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.18.0
pkgrel=2
pkgdesc="GStreamer Multimedia Framework Bad Plugins (mingw-w64)"
arch=('any')
url="https://gstreamer.freedesktop.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-shaderc"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc")
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-chromaprint"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-faad2"
         "${MINGW_PACKAGE_PREFIX}-faac"
         "${MINGW_PACKAGE_PREFIX}-fdk-aac"
         "${MINGW_PACKAGE_PREFIX}-fluidsynth"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbs2b"
         "${MINGW_PACKAGE_PREFIX}-libdca"
         "${MINGW_PACKAGE_PREFIX}-libdvdnav"
         "${MINGW_PACKAGE_PREFIX}-libdvdread"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libgme"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-libmpeg2"
         "${MINGW_PACKAGE_PREFIX}-libnice"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libsrtp"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libmicrodns"
         "${MINGW_PACKAGE_PREFIX}-nettle"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openh264"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-orc"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-rtmpdump-git"
         "${MINGW_PACKAGE_PREFIX}-soundtouch"
         "${MINGW_PACKAGE_PREFIX}-srt"
         "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
         "${MINGW_PACKAGE_PREFIX}-vulkan-validation-layers"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-zbar")
options=(!libtool strip staticlibs)
source=(${url}/src/${_realname}/${_realname}-${pkgver}.tar.xz
        "0001-meson-fix-vulkan-detection.patch"
        "0002-fix-vulkan-mkenum.patch")
sha256sums=('f382ab1caddd64aaa7acb7c4787487f63fd39bd0fde9c757655cbaa457c1185f'
            'e038864a83bafc0e16f143f16a86215dd52621703c8272c580f43ab56d30110f'
            '5ca55fdfc4c5d10d2319f83dddac8130c2ffab67b1248d2233cdcb188d5a0fd3')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-meson-fix-vulkan-detection.patch"
  patch -Np1 -i "${srcdir}/0002-fix-vulkan-mkenum.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ${MINGW_PREFIX}/bin/meson.exe \
    --buildtype plain \
    --wrap-mode=nofallback \
    -Dauto_features=enabled \
    -Dpackage-origin='https://www.msys2.org' \
    -Dtests=disabled \
    -Ddoc=disabled \
    -Dexamples=disabled \
    -Dx11=disabled \
    -Dwayland=disabled \
    -Dbluez=disabled \
    -Ddvb=disabled \
    -Dfbdev=disabled \
    -Dipcpipeline=disabled \
    -Dkms=disabled \
    -Dmsdk=disabled \
    -Dopensles=disabled \
    -Dshm=disabled \
    -Dtinyalsa=disabled \
    -Duvch264=disabled \
    -Ddc1394=disabled \
    -Ddirectfb=disabled \
    -Dflite=disabled \
    -Dkate=disabled \
    -Dladspa=disabled \
    -Dlibde265=disabled \
    -Dlibmms=disabled \
    -Dlv2=disabled \
    -Dmpeg2enc=disabled \
    -Dmplex=disabled \
    -Dmusepack=disabled \
    -Dneon=disabled \
    -Dofa=disabled \
    -Dopenmpt=disabled \
    -Dopenni2=disabled \
    -Dsbc=disabled \
    -Dsctp=disabled \
    -Dspandsp=disabled \
    -Dteletext=disabled \
    -Dvoaacenc=disabled \
    -Dwebrtcdsp=disabled \
    -Dwildmidi=disabled \
    -Dwpe=disabled \
    -Diqa=disabled \
    -Dmagicleap=disabled \
    -Dmediafoundation=disabled \
    -Dv4l2codecs=disabled \
    -Dva=disabled \
    -Dwasapi2=disabled \
    -Davtp=disabled \
    -Dsvthevcenc=disabled \
    -Dzxing=disabled \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/ninja.exe
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}${MINGW_PREFIX}" ${MINGW_PREFIX}/bin/ninja.exe install

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/gstreamer*/*.a

  # Remove reference to build directory
  local PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
